/*****************************************************************************
 * ui_sys_head.cpp: Copyright 2023 (c) CoreLink
 * Desc:
 *
 * Author: Flying (weekend@111.com)
 * Date: 2023-09-06 15:09:08
 * Notes:
 *
 * ---------------------------------------------------------------------------
 * LastEditors:
 * LastEditTime:
 *****************************************************************************/
/*------------------------------- Includes ----------------------------------*/
#include "ui_sys_head.h"
#include "stdio.h"
#include "string.h"
#include "version.h"
#include <time.h>

#include "ui_common.h"

/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/
#define HEAD_HANDLE_SIZE (30 * MY_UI_H_ZOOM)

/*----------------------- 类型声明 ---------------------------------*/

/*----------------------- 变量声明 -----------------------------*/

/*----------------------- 函数原型 --------------------------------*/
static lv_obj_t *ui_sys_head_create_about(ui_sys_head_info_t *info);
static void set_date_str(ui_sys_head_info_t *info, char *buff);
static void ui_sys_head_show_date(lv_timer_t *t);

/*----------------------- 功能实现 --------------------------------*/

/*---------------------------------------------------------------------------*/
ui_sys_head_info_t *ui_sys_head_create(lv_obj_t *parent)
{
    ui_sys_head_info_t *info = (ui_sys_head_info_t *)MY_MALLOC(sizeof(ui_sys_head_info_t));
    memset(info, 0, sizeof(ui_sys_head_info_t));
    info->parent = parent;
    return info;
}

int ui_sys_head_show(ui_sys_head_info_t *info)
{
    if (!info)
    {
        return -1;
    }
    info->label_date = NULL;
    info->cont = lv_obj_create(info->parent);
    lv_obj_clear_flag(info->cont, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_clear_flag(info->cont, LV_OBJ_FLAG_SCROLL_ELASTIC);
    lv_obj_remove_style_all(info->cont);
    lv_obj_set_size(info->cont, MY_UI_W_MAX, MY_UI_H_MAX);
    /*设置滑动后的居中方式*/
    lv_obj_set_scroll_snap_y(info->cont, LV_SCROLL_SNAP_CENTER);

    info->vis_handle = lv_obj_create(info->cont);
    lv_obj_set_y(info->vis_handle, -HEAD_HANDLE_SIZE);
    lv_obj_set_size(info->vis_handle, MY_UI_W_MAX, MY_UI_H_MAX + HEAD_HANDLE_SIZE * 2);
    lv_obj_clear_flag(info->vis_handle, LV_OBJ_FLAG_SNAPPABLE);

    // lv_obj_set_style_bg_color(player, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(info->vis_handle, 0, 0);
    lv_obj_set_style_radius(info->vis_handle, 15 * MY_UI_H_ZOOM, 0);
    lv_obj_set_scroll_dir(info->vis_handle, LV_DIR_VER);
    lv_obj_set_style_border_width(info->vis_handle, 0, 0);
    lv_obj_set_style_border_side(info->vis_handle, LV_BORDER_SIDE_BOTTOM, 0);
    lv_obj_set_style_bg_opa(info->vis_handle, LV_OPA_70, 0);

    /* 透明obj,占位用*/
    info->placeholder1 = lv_obj_create(info->cont);
    lv_obj_remove_style_all(info->placeholder1);
    lv_obj_clear_flag(info->placeholder1, LV_OBJ_FLAG_CLICKABLE);
    // lv_obj_set_style_bg_color(info->placeholder1, lv_color_hex(0xff0000), 0);
    // lv_obj_set_style_bg_opa(info->placeholder1, LV_OPA_90, 0);

    info->placeholder2 = lv_obj_create(info->cont);
    lv_obj_remove_style_all(info->placeholder2);
    lv_obj_clear_flag(info->placeholder2, LV_OBJ_FLAG_CLICKABLE);
    // lv_obj_set_style_bg_color(info->placeholder2, lv_color_hex(0x00ff00), 0);
    // lv_obj_set_style_bg_opa(info->placeholder2, LV_OPA_90, 0);

    lv_obj_set_size(info->placeholder1, MY_UI_W_MAX, MY_UI_H_MAX);
    lv_obj_set_y(info->placeholder1, 0);

    lv_obj_set_size(info->placeholder2, MY_UI_W_MAX, MY_UI_H_MAX - HEAD_HANDLE_SIZE * 2);
    lv_obj_set_y(info->placeholder2, MY_UI_H_MAX + HEAD_HANDLE_SIZE);

    ui_sys_head_reset(info, LV_ANIM_OFF);

    ui_sys_head_create_about(info);

    // info->date_timer = lv_timer_create(ui_sys_head_show_date, 1000, info);
    // lv_timer_ready(info->date_timer);
    return 0;
}

/**
 * @description:
 * @param {*}
 * @return {*}
 */
int ui_sys_head_delete(ui_sys_head_info_t *info)
{
    if (info->date_timer)
    {
        lv_timer_del(info->date_timer);
    }
    if (info->cont)
    {
        lv_obj_clean(info->cont);
    }
    MY_FREE(info);
    return 0;
}

static lv_obj_t *ui_sys_head_create_about(ui_sys_head_info_t *info)
{
    lv_obj_t *item_cont = lv_obj_create(info->vis_handle);
    lv_obj_remove_style_all(item_cont);
    lv_obj_set_pos(item_cont, 0, HEAD_HANDLE_SIZE);
    lv_obj_set_size(item_cont, MY_UI_W_MAX, MY_UI_H_MAX);
    lv_obj_set_flex_flow(item_cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(item_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    {
        lv_obj_t *label = lv_label_create(item_cont);
        lv_obj_set_style_text_font(label, ui_font_get_font(40 * MY_UI_H_ZOOM), 0);
        lv_label_set_text(label, "关于");
    }

    {
        lv_obj_t *label = lv_label_create(item_cont);
        lv_obj_set_style_text_font(label, ui_font_get_font(28 * MY_UI_H_ZOOM), 0);
        lv_label_set_text_fmt(label, "作者: flying(V%d.%d.%d)", lv_version_major(), lv_version_minor(), lv_version_patch());
    }

    {
        lv_obj_t *label = lv_label_create(item_cont);
        lv_obj_set_style_text_font(label, ui_font_get_font(28 * MY_UI_H_ZOOM), 0);
        lv_label_set_text_fmt(label, "软件版本: V%s", cl_get_version_full_string());
    }

    {
        lv_obj_t *label = lv_label_create(item_cont);
        lv_obj_set_style_text_font(label, ui_font_get_font(28 * MY_UI_H_ZOOM), 0);
        lv_label_set_text_fmt(label, "版权所有: weekend@111.com");
    }

    // {
    //     lv_obj_t *label = lv_label_create(item_cont);
    //     lv_obj_set_style_text_font(label, ui_font_get_font(28 * MY_UI_H_ZOOM), 0);
    //     lv_label_set_text_fmt(label, "LVGL core: V%d.%d.%d", lv_version_major(), lv_version_minor(), lv_version_patch());
    // }

    {
        lv_obj_t *label = lv_label_create(item_cont);
        lv_obj_set_style_text_font(label, ui_font_get_font(28 * MY_UI_H_ZOOM), 0);
        lv_label_set_text_static(label, "https://gitee.com/mFlying/tos_lv_demo_hub");
    }

    // {
    //     lv_obj_t *obj = lv_rlottie_create_from_file(item_cont, 180 * MY_UI_W_ZOOM, 180 * MY_UI_H_ZOOM, MY_LOTTIE_PATH "about.json");
    // }

    // {
    //     lv_obj_t *obj = lv_rlottie_create_from_file(item_cont, 180 * MY_UI_W_ZOOM, 180 * MY_UI_H_ZOOM, MY_LOTTIE_PATH "scroll-top.json");
    // }

    return item_cont;
}

int ui_sys_head_reset(ui_sys_head_info_t *info, lv_anim_enable_t anim_en)
{
    if (!info)
    {
        return -1;
    }
    lv_obj_scroll_to_view(info->placeholder2, anim_en);
    return 0;
}

/**
 * @description:
 * @param {lv_timer_t} *t
 * @return {*}
 */
static void ui_sys_head_show_date(lv_timer_t *t)
{
    ui_sys_head_info_t *info = (ui_sys_head_info_t *)t->user_data;

    time_t timep;
    struct tm *p;
    time(&timep);
    p = gmtime(&timep);
    char buff[128];
    sprintf(buff, "%02d.%02d.%d %02d:%02d:%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
    set_date_str(info, buff);
}

/**
 * @description:
 * @param {char} *buff
 * @return {*}
 */
static void set_date_str(ui_sys_head_info_t *info, char *buff)
{
    if (info->label_date)
    {
        lv_label_set_text(info->label_date, buff);
        return;
    }
    info->label_date = lv_label_create(info->vis_handle);
    lv_obj_set_style_text_font(info->label_date, ui_font_get_font(14 * MY_UI_W_ZOOM), 0);
    lv_obj_align(info->label_date, LV_ALIGN_BOTTOM_LEFT, 10 * MY_UI_H_ZOOM, -5 * MY_UI_H_ZOOM);
    lv_label_set_text(info->label_date, buff);
}
